<template>
  <Form
    ref="loginForm"
    :model="form"
    :rules="rules"
    @keydown.enter.native="handleSubmit"
  >
    <FormItem prop="userName">
      <Input v-model="form.userName" placeholder="请输入用户名">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="pwd">
      <Input type="password" v-model="form.pwd" placeholder="请输入密码">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem>
      <div @click="handleSubmit" class="btn">登录</div>
    </FormItem>
    <div class="remeber">
      <Checkbox v-model="form.single" @on-change="changeCheck"
        >记住账号</Checkbox
      >
    </div>
  </Form>
</template>
<script>

export default {
  name: "LoginForm",
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: "账号不能为空", trigger: "blur" }];
      },
    },
    pwdRules: {
      type: Array,
      default: () => {
        return [
          {
            required: true,
            min: 6,
            message: "密码长度不能小于6位",
            trigger: "blur",
          },
        ];
      },
    },
  },
  data() {
    return {
      form: {
        userName: "",
        pwd: "",
        single: true,
      },
    };
  },
  created() {
    if (this.getCookie("userName")) {
      this.form.userName = this.getCookie("userName");
    }
    if (this.getCookie("pwd")) {
      this.form.pwd = this.getCookie("pwd");
    }
  },
  computed: {
    rules() {
      return {
        userName: this.userNameRules,
        pwd: this.pwdRules,
      };
    },
  },
  methods: {
    handleSubmit() {
      if (this.form.single) {
        this.setCookie("userName", this.form.userName, 7);
        this.setCookie("pwd", this.form.pwd, 7);
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit("on-success-valid", {
            userName: this.form.userName,
            pwd: this.form.pwd,
          });
        }
      });
    },
    changeCheck() {
      this.form.single = !this.form.single;
      if (this.form.single) {
        this.setCookie("userName", this.form.userName, 7);
        this.setCookie("pwd", this.form.pwd, 7);
      }
    },
    setCookie(name, value, day) {
      let date = new Date();
      date.setDate(date.getDate() + day);
      document.cookie = name + "=" + value + ";expires=" + date;
    },
    getCookie(name) {
      var reg = RegExp(name + "=([^;]+)");
      var arr = document.cookie.match(reg);
      if (arr) {
        return arr[1];
      } else {
        return "";
      }
    },
  },
};
</script>
<style lang="less" scoped>
.btn {
  width: 100%;
  height: 30px;
  background: #b71124;
  border-radius: 5px;
  color: #fff;
  font-size: 9px;
  text-align: center;
  line-height: 30px;
  margin-top: 10px;
  cursor: pointer;
}
/deep/.ivu-checkbox-checked .ivu-checkbox-inner{
  border-color: #b71124;
  background: #b71124;
}

</style>
